﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

// gcmp headers
#include "CommandParameters.h"
#include "IEditMode.h"

// gcs headers
#include "IGcsAddinContext.h"
#include "IGcsServiceFactory.h"
#include "IGcsViewService.h"
#include "PredefinedGcsLocations.h"
#include "PredefinedGcsEntrances.h"

// gmep headers
#include "CmdShowGjwMainWindow.h"
#include "CommandIds.h"
#include "CommandRegister.h"
#include "ModuleNames.h"
#include "GapDocumentStatesManager.h"

// last headers
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 


using namespace gmepgcs;
using namespace gcmp;
using namespace gmep;

REGISTER_COMMAND(GCS_MODULE_NAME, CmdShowGjwMainWindow)


CmdShowGjwMainWindow::CmdShowGjwMainWindow() : gmep::GmepCommandBase(ID_SHOW_GJW_MAINWINDOW_COMMAND,true)
{
}

CmdShowGjwMainWindow::~CmdShowGjwMainWindow()
{

}

bool CmdShowGjwMainWindow::IsEnabledImpl() const
{
    const gcmp::IDocument* pDoc = GetCurrentDocument();
    if (!pDoc)
    {
        return false;
    }  
    if (IEditMode::GetTopActiveEditMode(pDoc))
    {
        return false;
    }
    return  true;
}

bool CmdShowGjwMainWindow::IsVisibleImpl() const
{
    return true;
}

OwnerPtr<IAction> CmdShowGjwMainWindow::ExecuteCommand(const CommandParameters& cmdParams)
{
    gcs::IGcsAddinContext *pGcsAddinContext = gcs::IGcsAddinContext::Get();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pGcsAddinContext, L"pGcsAddinContext为空", L"GMEP", L"2024-01-30");

    gcs::IGcsServiceFactory *pGcsServiceFactory = pGcsAddinContext->ServiceFactory();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pGcsServiceFactory, L"pGcsServiceFactory为空", L"GMEP", L"2024-01-30");

    gcs::IGcsViewService *pGcsViewService = pGcsServiceFactory->GetService<gcs::IGcsViewService>();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pGcsViewService, L"pGcsViewService为空", L"GMEP", L"2024-01-30");
    
    // edition
    gcs::EGcsEdition edition = GetDocumentState() == gap::DocumentStates::Native
        ? gcs::EGcsEdition::Standalone
        : gcs::EGcsEdition::Collaboration;

    // target location
    gcs::GcsLocation targetLocation = gcs::PredefinedGcsLocations::LCT_COMPONENT_ALL;

    // from entrance
    gcs::GcsEntrance fromEntrance = gcs::PredefinedGcsEntrances::ENT_GMEP_TOOLBAR_GJW;

    // show
    pGcsViewService->ShowMainWindow(edition, targetLocation, fromEntrance);

    return nullptr;
}